<template>
  <a-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    :confirm-loading="btnLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
  >
    <a-form
      layout="horizontal"
      :label-col="{ span: 5 }"
      :wrapper-col="{ span: 15 }"
      :form="form"
      @keyup.enter.native="handleOk"
    >
      <a-form-item label="新密码">
        <a-input
          allowClear
          type="password"
          placeholder="请输入"
          class="per100"
          v-decorator="['password', { rules: [{ required: true, message: '请输入新密码' }] }]"
        ></a-input>
      </a-form-item>
      <a-form-item label="确认密码">
        <a-input
          allowClear
          type="password"
          placeholder="请输入"
          class="per100"
          v-decorator="[
            'confirmpassword',
            {
              rules: [
                { required: true, message: '请输入确认密码' },
                {
                  validator: this.compareToFirstPassword
                }
              ]
            }
          ]"
        ></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import CommonApi from '@/api/Common/index'
import { postAction } from '../../../api/manage'
export default {
  name: 'ResetPassword',
  data() {
    return {
      title: '校验操作密码',
      modalWidth: 500,
      visible: false,
      form: this.$form.createForm(this),
      id: 0,
      btnLoading: false,
      username: ''
    }
  },
  created() {},
  methods: {
    // 展示弹窗
    showModal(record) {
      this.visible = true
      this.id = record.id
      this.username = record.username
    },

    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          const data = { ...values, username: this.username }
          this.btnLoading = true
          postAction(CommonApi.ResetPassword, data).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message)
              this.close()
              this.$emit('ok')
            } else {
              this.$message.error(res.message)
            }
            this.btnLoading = false
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.form.resetFields()
      this.visible = false
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('password')) {
        callback('两次输入的密码不一样！')
      } else {
        callback()
      }
    }
  }
}
</script>

